import { Table, TableOperate, IColumnRender } from 'spd-components/table';
import { useState } from 'react';
import { IQueryOrganizationUnitsUsersStruct } from './user-list.logic';
import { message } from 'antd';
// import { changeUserActive } from '../rquests';
import { Account } from 'spd-request/account';
import { User } from 'spd-request/user';
import { AddButton, Switch } from 'spd-components/buttons';
export function useTableHeader() {
    let [columns] = useState(() => {
        const changeActiveStatusRender: IColumnRender<'isActive', IQueryOrganizationUnitsUsersStruct> = (value, obj) => {
            return (
                <Switch
                defaultCheck={value}
                onclick={
                    () => {
                        return User.updateActiveStatus(obj.id, !obj.isActive).then(res => {
                            if(res.error === null) {
                                return !obj.isActive;
                            } else {
                                message.error(res.error);
                                return obj.isActive;
                            }
                        });
                    }
                }
                onValueChange={(val) => { obj.isActive = val }} />
            );
        }
        
        const operateRender: IColumnRender<'id', IQueryOrganizationUnitsUsersStruct> = (id, obj) => {
            return (
                <TableOperate.Container>
                    <TableOperate.Reset
                    title="确认重置密码为123456？"
                    onConfirm={() => {
                        return Account.resetPasswordByAdmin(id).then(({error}) => {
                            if(error == null) {
                                message.success('重置密码成功！');
                            } else {
                                message.error('重置密码失败！');
                            }
                        });
                    }}
                    ></TableOperate.Reset>
                    <TableOperate.Edite></TableOperate.Edite>
                    <TableOperate.Delete 
                    onConfirm={() => { }}
                    ></TableOperate.Delete>
                </TableOperate.Container>
            )
        }
        return [
            {
                title: '序号',
                dataIndex: 'idx',
                width: '70px'
            },
            {
                title: '姓名',
                dataIndex: 'name',
                with: '120px'
            },
            {
                title: '账号',
                dataIndex: 'userName',
            },
            {
                title: '电话',
                dataIndex: 'phoneNumber',
            },
            {
                title: '部门',
                dataIndex: 'displayName',
            },
            {
                title: '角色',
                dataIndex: 'assignedRoleDisplayNames',
                render: (value: string[]) => value.join(' ')
            },
            {
                title: '状态',
                dataIndex: 'isActive',
                render: changeActiveStatusRender
            },
            {
                title: '操作',
                dataIndex: 'idx',
                key: 'operate',
                render: operateRender
            }
        ];
    });
    return [columns]
}